﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VINETLIFE.DO;
using VINETLIFE.BL;
using System.Data;

namespace VINETLIFE.Module
{
    public partial class ucLeftMenuProductCate : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            //{
            // rptLeftMenuProductDataSource();
            rptMainMenuDataSource();
            // }
        }

        //private void rptLeftMenuProductDataSource()
        //{
        //    rptLeftMenuProduct.DataSource = new cmsProductCategoryBL().SelectAll();
        //    rptLeftMenuProduct.DataBind();
        //}
        private void rptMainMenuDataSource()
        {
            
            rptMainMenu.DataSource = new cmsMenuBL().SelectMenu(3, 0);
            rptMainMenu.DataBind();
        }
        protected string FriendlyUrl(string s)
        {
            return Ultility.Change_AV(s);
        }
        cmsMenuDO objMenu = new cmsMenuDO();
        cmsMenuBL menuBL = new cmsMenuBL();
        protected void rptMainMenu_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            int categoryID = -1;
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                int number;
                bool isNumber = int.TryParse(Request.QueryString["ID"], out number);
                if (!isNumber) return;
                else
                    categoryID = int.Parse(Request.QueryString["ID"]);
            }
            string url = Request.Url.AbsolutePath;
            url = url.Substring(1, url.Length - 1);
            string url1 = url.Replace(".", "/");
            cmsMenuDO objMN = new cmsMenuBL().SelectByURL("/"+url);
           
            string Module = url1.Substring(0, url1.IndexOf("/"));
            if (Module.Equals("") || Module.ToLower().Equals("default") || Module.ToLower().Equals("xe-dap") || Module.ToLower().Equals("nha-san-xuat") || Module.ToLower().Equals("gia-san-pham") || Module.ToLower().Equals("noi-dung") || Module.ToLower().Equals("danh-muc-tin") || Module.ToLower().Equals("bai-viet") || Module.ToLower().Equals("san-pham-moi") || Module.ToLower().Equals("khuyen-mai"))
            { }
            else
            {
                if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
                {
                    DataRowView drv = (DataRowView)e.Item.DataItem;
                    //DataTable dt = new cmsMenuBL().se

                    Repeater rptChildMenu = (Repeater)e.Item.FindControl("rptChildMenu");
                    int parentID = 0;
                    string URLParent = drv["Link"].ToString();
                    string PRLink = "";
                    if (objMN.ParentID == 0) //vao tm cha
                    {
                        parentID = int.Parse(drv["MenuID"].ToString());
                        PRLink = objMN.Link;
                    }
                    else // vao tm con
                    {
                        parentID = objMN.ParentID;
                        PRLink = new cmsMenuBL().Select(new cmsMenuDO { MenuID = objMN.ParentID }).Link;
                        
                    }//objMenu.MenuID = parentID; // cha
                    //objMenu = menuBL.Select(objMenu);
                    
                    try
                    {

                        if (URLParent == PRLink)
                            {
                                rptChildMenu.DataSource = new cmsMenuBL().SelectMenu(3, parentID);
                                rptChildMenu.DataBind();
                            }
                       
                    }
                    catch (Exception)
                    {

                        throw;
                    }
                    
                }
            }
        }
    }
}